package com.ciwong.epaper.modules.bookstore.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.common.animdialogactivity.ViewInflater;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.evaluate.bean.ShareBean;
import com.ciwong.epaper.modules.me.bean.MedalsBean;
import com.ciwong.epaper.modules.share.QQShareHelper;
import com.ciwong.epaper.modules.share.WxShareHelper;
import com.ciwong.epaper.modules.share.bean.ShareMsgObj;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.FileUtil;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.Xml2BitmapUtils;
import com.ciwong.epaper.widget.NewLoginDialog;
import com.ciwong.epaper.widget.ShareDialogResult;
import com.ciwong.eventbus.EventBus;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.i.GoBackListener;
import com.google.gson.Gson;
import com.tencent.connect.common.Constants;

/**
 * Created by keepon on 2018/11/28.
 */
public class DuiBaActivity extends BaseHtmlActicity {
    String mUrl;
    private Gson gson;
    private Object view;
    private UserInfoBase userInfoBase;
    private SubmitEvent shareData;
    private ImageButton imageButton, sharbtn;
    private ViewGroup webviewlay;


    @Override
    protected void preCreate() {
        super.preCreate();
        mUrl = getIntent().getStringExtra(IntentFlag.INTENT_FLAG_URL);
    }

    @Override
    protected void init() {
        super.init();
        setValideSource(false);
        EventBus.getDefault().register(this);
        userInfoBase = EApplication.getInstance().getUserInfoBase();
        hideTitleBar();

    }

    @Override
    protected void findViews() {
        super.findViews();
        try {
            webviewlay = findViewById(R.id.ll_book_info);
            findViewById(R.id.duiba_title).setVisibility(View.VISIBLE);
            findViewById(R.id.duiba_back).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            imageButton = findViewById(R.id.duiba_go_back_img);
            sharbtn = findViewById(R.id.duiba_right_btn);
            imageButton.setVisibility(View.VISIBLE);
            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DuiBaActivity.this.goBack();

                }
            });
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                DuiBaActivity.this.goBack();
            }
        });

    }

    @Override
    protected void loadData() {
        super.loadData();

        if (!TextUtils.isEmpty(mUrl)) {
            setStartURL(mUrl);
            loadUrl(startURL.toString());
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        WxShareHelper.getInstance().onResumeResultData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_QQ_SHARE || requestCode == Constants.REQUEST_QZONE_SHARE) {
            QQShareHelper.getInstance().onActivityResultData(requestCode, resultCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(SubmitEvent submitEvent) {
        if (submitEvent.getId().equals("GetShareInfo")) { //标题栏显示分享按钮
            setShareData(submitEvent);

        }
    }

    public void setShareData(SubmitEvent submitEvent) {
        try {
            if (gson == null)
                gson = new Gson();
            MedalsBean medalsBean = gson.fromJson(submitEvent.getJson(), MedalsBean.class);
            if (medalsBean != null && medalsBean.getIsShow()) {
                makeShareBitmap(medalsBean);
                setShareButton(medalsBean);
            } else {
                sharbtn.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    private boolean flagMakeBitmapSuccess = false;

    private void makeShareBitmap(MedalsBean medalsBean) {
        flagMakeBitmapSuccess = false;
        showCricleProgress();
        ViewInflater.getViewWithData(DuiBaActivity.this, medalsBean, userInfoBase, new ViewInflater.CallBack() {
            @Override
            public void callback(boolean makeBitmapSuccess, View view) {
                hideCricleProgress();
                flagMakeBitmapSuccess = makeBitmapSuccess;
                if (makeBitmapSuccess)
                Xml2BitmapUtils.saveCroppedImage(DuiBaActivity.this, view, FileUtil.makeShareIntegralDialogLocalPath(DuiBaActivity.this));
            }
        });

    }
    private void makeShareBitmap2(MedalsBean medalsBean) {
        flagMakeBitmapSuccess = false;
        ViewInflater.getViewWithData(DuiBaActivity.this, medalsBean, userInfoBase, new ViewInflater.CallBack() {
            @Override
            public void callback(boolean makeBitmapSuccess, View view) {
                hideCricleProgress();
                if (makeBitmapSuccess)
                Xml2BitmapUtils.saveCroppedImage(DuiBaActivity.this, view, FileUtil.makeShareIntegralDialogLocalPath(DuiBaActivity.this));
            }
        });

    }

    /**
     * 设置分享按钮（展示与隐藏）
     *
     * @param medalsBean
     */
    private void setShareButton(final MedalsBean medalsBean) {
        sharbtn.setVisibility(View.VISIBLE);
        sharbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flagMakeBitmapSuccess == true) {
                    if (EConstants.IS_YOUKE) {
                        NewLoginDialog newLoginDialog = new NewLoginDialog(DuiBaActivity.this);
                        newLoginDialog.showLoginDialog();
                    } else {
                        showShareDialog();
                    }
                } else {//当分享的图片还未生成，再次去生成
                    makeShareBitmap2(medalsBean);
                }
            }
        });
    }

    private void showShareDialog() {//展示分享dialog
        ShareBean shareBean = getShareBean(DuiBaActivity.this);
        ShareDialogResult shareDialog = new ShareDialogResult(DuiBaActivity.this);
        shareDialog.setShareBean(shareBean);
        shareDialog.show();
    }

    private static ShareBean getShareBean(Context context) {
        ShareBean shareBean = new ShareBean();
        shareBean.setShareType(ShareMsgObj.ShareType.SHARE_IMAGE);
        shareBean.setFileLocalPath(FileUtil.makeShareIntegralDialogLocalPath(context.getApplicationContext()));
        shareBean.setTitle("勋章详情");
        return shareBean;
    }

}
